package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.RatingContent;
import com.ruoyi.system.domain.UserScore;
import com.ruoyi.system.mapper.RatingContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RatingCategoryMapper;
import com.ruoyi.system.domain.RatingCategory;
import com.ruoyi.system.service.IRatingCategoryService;

/**
 * 分类父子信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class RatingCategoryServiceImpl implements IRatingCategoryService 
{
    @Autowired
    private RatingCategoryMapper ratingCategoryMapper;
    @Autowired
    private RatingContentMapper ratingContentMapper;

    /**
     * 查询分类父子信息
     * 
     * @param categoryId 分类父子信息主键
     * @return 分类父子信息
     */
    @Override
    public RatingCategory selectRatingCategoryByCategoryId(Long categoryId)
    {
        return ratingCategoryMapper.selectRatingCategoryByCategoryId(categoryId);
    }
    /**
     * 查询分类父子信息列表
     * 
     * @param ratingCategory 分类父子信息
     * @return 分类父子信息
     */
    @Override
    public List<RatingCategory> selectRatingCategoryList(RatingCategory ratingCategory)
    {
        return ratingCategoryMapper.selectRatingCategoryList(ratingCategory);
    }

    /**
     * 新增分类父子信息
     * 
     * @param ratingCategory 分类父子信息
     * @return 结果
     */
    @Override
    public int insertRatingCategory(RatingCategory ratingCategory)
    {
        return ratingCategoryMapper.insertRatingCategory(ratingCategory);
    }

    /**
     * 修改分类父子信息
     * 
     * @param ratingCategory 分类父子信息
     * @return 结果
     */
    @Override
    public int updateRatingCategory(RatingCategory ratingCategory)
    {
        return ratingCategoryMapper.updateRatingCategory(ratingCategory);
    }

    /**
     * 批量删除分类父子信息
     * 
     * @param categoryIds 需要删除的分类父子信息主键
     * @return 结果
     */
    @Override
    public int deleteRatingCategoryByCategoryIds(Long[] categoryIds)
    {
        return ratingCategoryMapper.deleteRatingCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除分类父子信息信息
     * 
     * @param categoryId 分类父子信息主键
     * @return 结果
     */
    @Override
    public int deleteRatingCategoryByCategoryId(Long categoryId)
    {
        return ratingCategoryMapper.deleteRatingCategoryByCategoryId(categoryId);
    }

    @Override
    public List<RatingCategory> selectAllCategory() {
        List<RatingCategory> list = ratingCategoryMapper.selectRatingCategory();
        // 构建树形结构
        return buildCategoryTree(list);
    }

    @Override
    public PageInfo<RatingContent> selectCategoryByCategory(RatingCategory ratingCategory) {
        PageHelper.startPage(ratingCategory.getPageNum(), ratingCategory.getPageSize());
        List<RatingContent> ratingContentPageInfo = ratingContentMapper.selectCategoryByCategory(ratingCategory);

        return new PageInfo<>(ratingContentPageInfo);
    }
    /**
 * 根据分类列表构建树形结构
 *
 * @param categoryList 分类列表
 * @return 返回根节点列表，即树形结构的入口
 */
public List<RatingCategory> buildCategoryTree(List<RatingCategory> categoryList) {
    List<RatingCategory> rootNodes = new ArrayList<>();
    Map<Long, RatingCategory> categoryMap = new HashMap<>();

    // 1. 构建分类映射关系
    for (RatingCategory category : categoryList) {
        categoryMap.put(category.getCategoryId(), category);  // 将 categoryId 存入 categoryMap
        // 如果 parentId 为 0，说明是根节点
        if (category.getParentId() == 0) {
            rootNodes.add(category);  // 父节点为0，视为根节点
        }
    }

    // 2. 建立父子关系并递归构建树形结构
    for (RatingCategory category : categoryList) {
        if (category.getParentId() != 0) {
            RatingCategory parentCategory = categoryMap.get(category.getParentId());
            if (parentCategory != null) {
                // 把当前分类（子节点）添加到父节点的 children 中
                parentCategory.getContents().add(category);
            }
        }
    }

    // 3. 返回根节点，即包含了完整树形结构的分类列表
    return rootNodes;
}





}
